import axios from "axios";
import {getAuthorUrl, getTagQueryUrl} from "../../constant/pixivUrls.js";

/**
 * 查询标签信息
 * @param tagName {string}
 * @returns {Promise<PixpediaTag>}
 */
export async function searchTags(tagName) {
    const url = getTagQueryUrl(tagName)

    const response = await axios.get(url)
    if (response.data.error !== false) {
        throw new Error('查询标签信息失败！response: ', response)
    }
    const pixpedia = response.data.body.pixpedia
    if (Object.hasOwnProperty.call(pixpedia, 'id')) {
        return pixpedia
    } else {
        return {
            id: pixpedia.tag,
            tag: pixpedia.tag
        }
    }
}

/**
 * 根据userId查询作者
 * @param userId
 * @return {Promise<RawUserInfo>}
 */
export async function getAuthorInfoById(userId) {
    const url = getAuthorUrl(userId)

    const response = await axios.get(url)
    if (response.data.error !== false) {
        throw new Error('查询作者信息失败！response: ', response)
    }
    return response.data.body
}
